/**
 * 附件模块
 * @class AttachmentGrid
 * @extends Ext.grid.GridPanel
 */
AttachmentGrid = Ext.extend(Ext.grid.GridPanel, {
	store: null,
	cm: null,
	uploadWin: null,
	mastInsId: 0,
	auth: true,
	constructor: function(config){
		Ext.apply(this, config);
		
		function renderAttachmentName(value, cellmeta, record, rowIndex, columnIndex, store){
			var attachmentId = record.data["attachment_ID"]
		
			value = "<a href=\""+window.basePath+"/file/download.htm?attachmentId="+attachmentId+"\" >"+value+"</a>"
			return value;
		}
		
		function renderIcon(value){		
			value = "<img src='"+window.basePath+"/images/pdm/attach.png' alt=\"attachment\" />"
			return value;
		}
		
		this.cm = new Ext.grid.ColumnModel([
			{dataIndex: 'attachment_ID', hidden:true},
			new Ext.grid.RowNumberer(),
			{header: " ", width:35, renderer: renderIcon },
            {header: "名称", width:500, dataIndex: 'fileName', renderer: renderAttachmentName},
            {header: "大小", width:80, dataIndex: 'size'},
            {header: "上传人员", width:160, dataIndex: 'ownerName'}
		]);
		
		this.store = new Ext.data.Store({
			autoLoad: true,
			baseParams: this.condition, 
		    proxy: new Ext.data.HttpProxy({
		    	method: 'POST',  
			 	url: '/pdm/file/attachment.htm?mastInsId='+this.mastInsId
			}),
			reader: new Ext.data.JsonReader({
				totalProperty: 'totalProperty',
				root: 'root',
				id: 'attachment_ID'
			},[
				{name: 'attachment_ID'},
				{name: 'fileName'},
				{name: 'size'},
				{name: 'ownerName'},
				{name: 'owner_FK'}
			])
		});
				
		AttachmentGrid.superclass.constructor.call(this, {
			store: this.store,
	        cm: this.cm,
	        autoHeight: true,
	        anchor: '96%',
	        viewConfig: {forceFit: true},
	        tbar:[{
				text: "增加",
				tooltip: "增加相关的附件",
				iconCls: 'add',
				scope: this,
				disabled: !this.auth,
				handler: function(){
					if(Ext.isEmpty(this.uploadWin)){
						this.uploadWin = new FileUploadWindow({mastId: this.mastInsId, recallgrid: this});
					}
					this.uploadWin.show();
				}
			},'-',{
				text: "删除",
				tooltip: "删除相关的附件",
				iconCls: 'delete',
				scope: this,
				disabled: !this.auth,
				handler: function(){
					var record = this.getSelectionModel().getSelected();
					if(!record){
						Ext.Msg.alert("提示","请先选中需要删除的文件!");
					}else{
						var attachmentId = record.get("attachment_ID");
						Ext.Ajax.request({
							url: '/pdm/file/deleteAttachment.htm',
							method: 'post',
							scope: this,
							params: {
								att_id: attachmentId
							},
							success: function(response,options){										
								this.reload();								
							},
							failure: function(response){
								var result = Ext.decode(response.responseText).message;
								Ext.Msg.alert("信息:", result);
							}
						});
						
					}
				}
			}]
		});
	},
	reload: function(){
		this.store.load();
	}
});

/**
 * 
 * @class FileUploadWindow
 * @extends Ext.Window
 */
FileUploadWindow = Ext.extend(Ext.Window, {
	mastId: 0,
	recallgrid: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		FileUploadWindow.superclass.constructor.call(this, {
			width: 500,
			height: 115,
			layout: 'fit',
			modal: true,
			border: false,
			closeAction: 'hide',
			bodyStyle: 'padding:0px;',
			buttonAlign: 'center',
			items: [{
				xtype: 'form',
				fileUpload: true,
		        width: 500,
		        frame: true,
		        labelAlign: 'right',
		        autoHeight: true,
		        bodyStyle: 'padding: 10px 10px 0 10px;',
		        labelWidth: 40,
				defaults: {
					anchor: '95%',
		            allowBlank: false,
		            msgTarget: 'side'
		        },
		        buttonAlign: 'center',
				items: [{
		            xtype: 'fileuploadfield',
		            id: 'uploadfileName',
		            name: 'uploadfileName',
		            emptyText: "请选择要上传附件",
		            fieldLabel: "附件",
		            buttonCfg: {
		                text: '',
		                iconCls: 'add'
		            }
				}]
			}],
	        buttons: [{
	        	text: "上传",
				iconCls: 'upload',
				scope: this,
				handler: function() 
				{
					var form = this.getForm().form;
					if(form.isValid()) {
						form.submit({
							url: '/pdm/file/upload.htm?mastInsId=' + this.mastId,
							method: 'POST',
							scope: this,
							success: function(gp, action)
							{
								this.hide();
								this.recallgrid.reload();
							},
							failure: function(gp, action) 
							{
								var msg = action.response.responseText;
								var obj = Ext.decode( msg );
								Ext.Msg.alert("温馨提示", obj.message);
							},
							waitMsg : "数据正在导入中，请稍后..."
						});
					}
					else Ext.Msg.alert("信息","请先选择附件!");
				}
			}]
		});
	},
	getForm: function(){
		return this.findByType("form", true)[0];
	},
	reset: function(){
		this.getForm().form.reset();
	}
});


 